package qc.module.platform.mapper;

import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.factory.Mappers;
import qc.common.core.enums.ColumnTypeEnum;
import qc.common.core.enums.DataTypeEnum;
import qc.common.core.enums.WhereConditionTypeEnum;
import qc.module.platform.dto.dbTableColumn.DBTableColumnDto;
import qc.module.platform.entity.QcDbTableColumn;

import java.util.List;

/**
 * DBTableColumnMapper
 *
 * @author QuCheng Tech
 * @since 2024/1/29
 */
@Mapper
public interface DBTableColumnMapper {
    DBTableColumnMapper MAPPER = Mappers.getMapper(DBTableColumnMapper.class);

    @Mapping(target = "coltype", ignore = true)
    @Mapping(target = "datatype", ignore = true)
    @Mapping(target = "qccmptype", ignore = true)
    QcDbTableColumn toEntity(DBTableColumnDto dto);

    /**
     * DTO转换为Entity
     *
     * @param dto 新增DTO
     * @return Entity
     * @author QuCheng Tech
     * @since 2023/5/28
     */
    default QcDbTableColumn dtoToEntity(DBTableColumnDto dto) {
        //DTO转换为Entity
        QcDbTableColumn en = DBTableColumnMapper.MAPPER.toEntity(dto);

        //处理数据类型，非空时进行转换，空值时使用默认
        Integer datatype = dto.getDatatype();
        //if (!StringUtils.isBlank(dto.getDatatype())) {
        //    try {
        //        datatype = Integer.parseInt(dto.getDatatype());
        //    } catch (NumberFormatException exception) {
        //        //转换错误不处理，使用默认状态
        //    }
        //}
        en.setDatatype(getDataTypeEnum(datatype));

        //处理列类型，非空时进行转换，空值时使用默认
        Integer coltype = dto.getColtype();
        //if (!StringUtils.isBlank(dto.getColtype())) {
        //    try {
        //        coltype = Integer.parseInt(dto.getColtype());
        //    } catch (NumberFormatException exception) {
        //        //转换错误不处理，使用默认状态
        //    }
        //}
        en.setColtype(getColumnTypeEnum(coltype));

        //处理查询条件比较类型，非空时进行转换，空值时使用默认
        Integer qccmptype = dto.getQccmptype();
        //if (!StringUtils.isBlank(dto.getQccmptype())) {
        //    try {
        //        qccmptype = Integer.parseInt(dto.getQccmptype());
        //    } catch (NumberFormatException exception) {
        //        //转换错误不处理，使用默认状态
        //    }
        //}
        en.setQccmptype(getWhereConditionTypeEnum(qccmptype));

        return en;
    }

    //@Mapping(target = "code", expression = "java(en.getCode().trim().toLowerCase(Locale.ROOT))")
    //@Mapping(target = "code", expression = "java(StringUtils.toRootLowerCase(StringUtils.trim(en.getCode())))")
    @Mapping(target = "coltype", source = "coltype.index")
    @Mapping(target = "datatype", source = "datatype.index")
    @Mapping(target = "qccmptype", source = "qccmptype.index")
    DBTableColumnDto toDto(QcDbTableColumn en);

    List<DBTableColumnDto> toDtoList(List<QcDbTableColumn> list);

    static DataTypeEnum getDataTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (DataTypeEnum item : DataTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return DataTypeEnum.STRING;
    }

    static ColumnTypeEnum getColumnTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (ColumnTypeEnum item : ColumnTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return ColumnTypeEnum.UN_DEFINE;
    }

    static WhereConditionTypeEnum getWhereConditionTypeEnum(Integer index) {
        if (index != null && index > 0x0) {
            for (WhereConditionTypeEnum item : WhereConditionTypeEnum.values()) {
                if (item.getIndex() == index) {//判断序号使用getIndex，判断内容使用getName
                    return item;
                }
            }
        }
        return WhereConditionTypeEnum.EQ;
    }
}
